package com.tuanzi.loan.web.bean.settlementsystem;

import com.google.common.collect.Lists;
import com.starbaba.model.utils.JSONUtils;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementBill;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementBillAnnex;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementIncome;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementPrepay;
import com.tuanzi.loan.business.service.settlementsystem.*;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFWorkflowBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:36
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BillCheckBean extends JSFWorkflowBean<SettlementBill> {
    @Autowired
    private SettlementBillService settlementBillService;

    @Autowired
    private SettlementIncomeService settlementIncomeService;

    @Autowired
    private SettlementPrepayService settlementPrepayService;

    @Autowired
    private SettlementBillAnnexService settlementBillAnnexService;

    private List<SettlementIncome> incomeList;

    private List<SettlementPrepay> prepayList;

    private List<SettlementBillAnnex> billAnnexList;

    private String annexUrl;

    @PostConstruct
    private void init() {
        if (task != null) {
            Long billId = Long.parseLong(task.getBusinessKey().split(":")[1]);
            $ = settlementBillService.findOne(billId);
            String detailList = $.getDetailList();
            if ($.getRecordType() == RemainType.EXPEND.index) {//消耗开票
                if (StringUtils.isNotBlank(detailList)) {
                    incomeList = JSONUtils.fromJsonArrayNew(detailList, SettlementIncome.class);
                } else {
                    incomeList = Lists.newArrayList();
                }
            } else if ($.getRecordType() == RemainType.PREPAY.index) {
                if (StringUtils.isNotBlank(detailList)) {
                    prepayList = JSONUtils.fromJsonArrayNew(detailList, SettlementPrepay.class);
                } else {
                    prepayList = Lists.newArrayList();
                }
            }
            billAnnexList = settlementBillAnnexService.findByBillId(billId);
            if (billAnnexList == null) billAnnexList = Lists.newArrayList();
        }
    }

    public void onSeeAnnex(Integer index){
        SettlementBillAnnex annex = billAnnexList.get(index);
        annexUrl = annex.getUrl();
        update("billCheck:annex-image");
    }

    @Override
    public void onSave() throws Exception{
        if ($.getBillStatus()==7 && StringUtils.isBlank($.getBillNumber())){
            throw new Exception("发票号为空");
        }
        $ = settlementBillService.saveOrUpdate($);
    }


    @Override
    public void validate() throws Exception{

    }

    @Override
    public String onApprove() {
        super.onApprove();
        return redirect("/views/settlementsystem/bill/bill-workbench.xhtml");
    }

    @Override
    public String onCancel() {
        super.onCancel();
        return redirect("/views/settlementsystem/bill/bill-workbench.xhtml");
    }

    @Override
    public String onPass() {
        String s = super.onPass();
        if (StringUtils.isBlank(s)){
            return null;
        }
        return redirect("/views/settlementsystem/bill/bill-workbench.xhtml");
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SETTLEMENT_SPREAD_CHANNEL, SETTLEMENT_INCOME_TYPE,
                SETTLEMENT_SPREAD_MODE, COOPERATION_MODE, SETTLEMENT_CYCLE,
                SETTLEMENT_BILLING_CONTENT, SETTLEMENT_BILL_TYPE, SETTLEMENT_PAY_MODE, SETTLEMENT_RECEIVE_COMPANY);
    }
}
